<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","skada_id"); //
$FileName       = "ais_setup_coa.php";
$FileTemplate   = "ais_setup_coa_form.html";
$SubHeaderTitle = "Setup Chart Of Account";
$TableName      = "tblcoa";
$HTMLFormName   = "ToolForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("ledger","skada","namacc","tipe","parent","grup","ktrasl","inc_ppn","untush","currency_type");
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");


//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
    if (GetParam("Action","")=="Add") {
        FormInsert();
    }
    if (GetParam("Action","")=="Update") {
        $FormMode="Edit";
        FormUpdate();
    }
    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        FormDelete();
    }
    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
}

//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
//dyah.060809
$VerifyFields  = array("ledger"=>"Ledger","skada"=>"Skada No","namacc"=>"Description","tipe"=>"Type","parent"=>"Parent COA","grup"=>"COA Group","untush"=>"Unit Usaha", "currency_type"=>"Currency Type");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName`";
if (GetParam(PRIMARY_KEY,"") && $FormMode="Edit") {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
} else {
  $KTRASL = GetOfficeID();
}

//Select COA
$SQLCOA = "SELECT * FROM `tblcoagroup`";
$SQLUU  = "SELECT * FROM tblactivity";
//Select Currency Table
$SQLCurr = "SELECT * FROM tblcurrency";

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
// Merge COA
$TBS->MergeBlock("groupblk",$DBMysql,$SQLCOA);
$TBS->MergeBlock("untushblk",$DBMysql,$SQLUU);
//Merge Currency Type
$TBS->MergeBlock("currformblk",$DBMysql,$SQLCurr);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
    global $DBConnection,$FileName;
    $DBConnection->Insert();
    header("Location: $FileName?PageMode=Form");
    exit;
}

function FormUpdate() {
    global $DBConnection,$TableName;
    $DBConnection->Update();
    $skada = GetParam('skada','');
    $currType = GetParam('currency_type','');
    $SQL = "UPDATE $TableName SET currency_type = '$currType' WHERE skada = '$skada'";
    $DBConnection->dbc->query($SQL);
}

function FormDelete() {
    global $DBConnection,$FileName,$TableName;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
    header("Location: $FileName");
    exit;
}

function FormSearch () {

}

function FormCancel () {
    global $FileName;
    header("Location: ".$FileName);
    exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        for ($x=0;$x<count($arrayfield);$x++) {
            $DBConnection->add_htmlfield($table,$arrayfield[$x],GetParam($arrayfield[$x],""));
        }
}

?>
